package com.szrt.day20170420.ch02;




import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

public class ListTest {
	
	
	@Test
	public void treeSetTest() throws Exception {
		
		TreeSet<Integer> tree = new TreeSet<Integer>();
		
		tree.add(10);
		tree.add(30);
		tree.add(15);
		tree.add(45);
		tree.add(50);
		
		Iterator<Integer> iterator = tree.iterator();
		
		while(iterator.hasNext()){
			Integer element = iterator.next();
			System.out.println(element);
		}
	}
	
	@Test
	public void setTest() throws Exception {
		
		
		//list的元素可以重复  set里面的元素不可以重复
		List<String> stringList = new ArrayList<String>();
		stringList.add("aa");
		stringList.add("bb");
		stringList.add("aa");
		
		System.out.println(stringList.size());
		
		
		//对比 Set
		Set<String>  sets = new HashSet<String>();
		sets.add("aa");
		sets.add("bb");
		sets.add("aa");
		
		
		// Collection
		System.out.println(sets.size());
		
		//迭代器
		Iterator<String> iterator = sets.iterator();
		while(iterator.hasNext()){
			String element = iterator.next();
			System.out.println(element);
		}
		
		
		//sets.clear();
		System.out.println(sets.size());
		
	}
	
	
	@Test
	public void removeTest() throws Exception {
		
			
		//2 地址相等  
		List<Student>  studentList = new LinkedList<Student>();
		
		Student jmj= new Student(23,"贾梦菊");
		Student zc = new Student(25,"周驰");
		
		
		Student zsh = new Student(30,"曾少华");
		
		
		studentList.add(jmj);
		studentList.add(zc);
		studentList.add(zsh);//0 1 2
		
		
		for(Student student:studentList){
			System.out.println(student);
		}
		
		
	}
	
	/**
	 * contains 默认比较的是 ==  Object的equals方法
	 * 但是 我们可以根据实际需要重写子类的equals方法
	 * @throws Exception
	 */
	@Test
	public void containsTest02() throws Exception {
		
		//2 地址相等  
		List<Student>  studentList = new LinkedList<Student>();
		
		Student jmj= new Student(23,"贾梦菊");
		
		//
		Student zmj= new Student(23,"张梦菊");
		
		
		Student zc = new Student(25,"周驰");
		
		studentList.add(jmj);
		studentList.add(zc);
		
		//stringList包含了"aa"  没有包含了"aaa"
		
		System.out.println(studentList.contains(zmj));
		
		System.out.println(studentList.get(0));
		
		
		
		
	}
	@Test
	public void containsTest01() throws Exception {
		
		//1 值相等    2 地址
		List<String>  stringList = new ArrayList<String>();
		
		stringList.add("aa");
		stringList.add("bb");
		
		//stringList包含了"aa"  没有包含了"aaa"
		
		System.out.println(stringList.contains("aa"));
		System.out.println(stringList.contains("aaa"));
		
		
	}
	
	@Test
	public void listTest03() throws Exception {
		List<Programmer>  programmerList = new ArrayList<Programmer>();
		
		Programmer jmj= new Student(23,"贾梦菊");
		Student zc = new Student(25,"周驰");
		programmerList.add(jmj);
		programmerList.add(zc);
		
		//1 基本用法 100%
		System.out.println("=================1======================");
		for(int index =0 ; index<programmerList.size();index++){
			Programmer programmer = programmerList.get(index);
			System.out.println(programmer);
		}
		
		//2 如果别人写了。你要看得懂  如果别人写错 你要能把错误修正  高级循环
		System.out.println("=================2======================");
		for(Programmer programmer:programmerList){
			System.out.println(programmer);
		}
		
		
		//3 不适合所有的类 比如数组   前提条件就是  Collection 的子类
		System.out.println("=================3======================");
		Iterator<Programmer> iterator = programmerList.iterator();
		while(iterator.hasNext()){
			//如果返回值为真  说明里面有元素
			Programmer programmer = iterator.next();
			System.out.println(programmer);
			
		}
	}
	
	/**
	 * List<Programmer>
	 * @throws Exception
	 */
	@Test
	public void listTest02() throws Exception {
		
		//在类型的后面接<类型：都是引用>
		List<String> super1704= new ArrayList<String>();
		super1704.add("aa");
		super1704.add("bb");
		
		List<Integer>  ageList = new ArrayList<Integer>();
		ageList.add(21);
		ageList.add(28);
		
		List<Student>  studentList = new ArrayList<Student>();
		
		Student jmj= new Student(23,"贾梦菊");
		Student zc = new Student(25,"周驰");
		
		studentList.add(jmj);
		studentList.add(zc);
		
		
		List<Programmer>  programmerList = new ArrayList<Programmer>();
		Programmer jmj2= new Student(23,"贾梦菊");
		
		Student zc2 = new Student(25,"周驰");
		
		programmerList.add(jmj2);
		System.out.println(programmerList.isEmpty());
		programmerList.add(zc2);
		
	}
	
	@Test
	public void listTest01() throws Exception {
		
		
		//数组的长度是固定 一旦确定就不能修改
		String[] class1704= new String[2];
		
		class1704[0]="aa";
		class1704[1]="bb";
		
		
		//List
		
		List super1704= new ArrayList();
		super1704.add("aa");
		super1704.add("bb");
		super1704.add("cc");
		
		int size = super1704.size();
		System.out.println(size);
		
		for(int index=0; index<size;index++){
			String object = (String)super1704.get(index);
			System.out.println(object);
			
		}
		
		super1704.remove("cc");
		
		
		System.out.println("删除cc后=================================");
		
		for(int index=0; index<super1704.size();index++){
			Object object = super1704.get(index);
			System.out.println(object);
			
		}
		
		
		//把  bb  改成  bbb
		
		super1704.set(1, "bbb");
		
		System.out.println("把  bb  改成  bbb=================================");
		
		for(int index=0; index<super1704.size();index++){
			Object object = super1704.get(index);
			System.out.println(object);
			
		}
		
		
		
		
		
		
	}

}
